import router from 'umi/router';

import React from 'react';
import { connect } from 'dva';
import Link from 'umi/link'
import { Table, Button, Form, Select } from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;


const { Option } = Select;

const FormItem = Form.Item;

const linePath = '/AccountMove/AccountMove';
//const formPath = '/AccountMove/AccountMoveForm';

@connect(({ accountMoveLine, accountTax }) => ({ accountMoveLine, accountTax }))
@Form.create()
class List extends React.Component {
  state = {
  }

  treeview = [
      {
        title: '',
        dataIndex: '_',
        render: (_, { id }) => {
          return <Link to={`${linePath}?id=${id}`}>查看</Link>;
        },
      },
      { title: '凭证号', dataIndex: 'move_id.name', },
      { title: 'id', dataIndex: 'id', },
      { title: '结转', dataIndex: 'full_reconcile_id.name', },
      { title: '日期', dataIndex: 'date', },
      { title: '科目代码', dataIndex: 'account_id.code', },
      { title: '科目名称', dataIndex: 'account_id.name', },
//      { title: '明细科目', dataIndex: 'sub_account_id.code', },
//      { title: '明细名称', dataIndex: 'sub_account_id.name', },
      { title: '借方', dataIndex: 'debit', },
      { title: '贷方', dataIndex: 'credit', },
      //{ title: '摘要', dataIndex: 'move_id.ref', },
      { title: '状态', dataIndex: 'move_id.state', },
//      { title: '到期', dataIndex: 'date_maturity', },
  ]

  async componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'accountMoveLine/search',
      payload: {domain:[
        '|',
        ['account_id.code','like','2221%'],
        ['journal_id.type','=','tax'],
      ],},
    });

    dispatch({
      type: 'accountTax/search',
      payload: {domain:[], },
    });

  }


  goto_createForm() {
      router.push('/Account/AccountMoveForm');
  }


  handleSubmit = e => {
    const { dispatch, form } = this.props;
    e.preventDefault();

    const carryover_vat = async (line_ids ) => {
        await dispatch({
          type: 'accountMove/carryover_vat',
          payload : { line_ids },
        });
    }

    const carryover_additional_tax = async (line_ids,tax_ids ) => {
        await dispatch({
          type: 'accountMove/carryover_additional_tax',
          payload : { line_ids, tax_ids },
        });
    }

    form.validateFieldsAndScroll( async (err, values) => {
      if (!err) {
        const { type, line_ids, tax_ids } = values;
        //console.log( values );
        if (type===1){
            await carryover_vat(line_ids )
        }

        if (type===2){
            await carryover_additional_tax( line_ids, tax_ids )
        }

      }
    })
  }


  render() {
    const { form: { getFieldDecorator }  } = this.props;

    const { accountMoveLine: { recordsList } } = this.props;
    const { accountTax: { recordsList: accountTaxList } } = this.props;

    //console.log(recordsList)

    return (
      <div>
          <Form onSubmit={this.handleSubmit} >
            <FormItem style={{ marginTop: 32 }}>
              <Button type="primary" htmlType="submit" >提交
              </Button>
            </FormItem>

            <FormItem label="功能">
              {getFieldDecorator('type', {
                //rules: [{ required: true }],
                //initialValue:

              })(
                <Select >
                  <Option value={1}>1 结转增值税</Option>
                  <Option value={2}>2 计算附加税</Option>
                </Select>

              )}
            </FormItem>


            <FormItem label="地方税">
              {getFieldDecorator('tax_ids', {
                //rules: [{ required: true }],
                //initialValue:

              })(
                <Select mode="multiple"
                  placeholder="" >
                  {accountTaxList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.id} ${item.name}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>


            <FormItem label="选择明细行">
              {getFieldDecorator('line_ids', {
                //rules: [{ required: true }],
                //initialValue:

              })(
                <Select mode="multiple"
                  placeholder="" >
                  {recordsList.map(item => {
                    return (
                      <Option value={item.id} key="id">
                        {`${item.id}`}
                      </Option>
                    );
                  })}

                </Select>

              )}
            </FormItem>

          </Form>


        <DescriptionList size="large" title='操作' style={{ marginBottom: 32 }}>
          <Button onClick={()=>this.goto_createForm()}>新建</Button>
        </DescriptionList>

        <Table columns={this.treeview} dataSource={recordsList} rowKey="id" />




        <DescriptionList size="large" title='出纳缴纳增值税' style={{ marginBottom: 32 }}>
          <Description term='' >{'出纳填写付款单'}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='会计处理增值税' style={{ marginBottom: 32 }}>
          <Description term='' >{'汇总所有对进项税、销项税，抵扣后，为应交增值税'}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='' style={{ marginBottom: 32 }}>
          <Description term='' >{'免税，则，制作一张转入营业外收入的凭证'}</Description>
        </DescriptionList>
        <DescriptionList size="large" title='' style={{ marginBottom: 32 }}>
          <Description term='' >{'无法抵扣的进项税，则，制作一张转入费用的凭证'}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='' style={{ marginBottom: 32 }}>
          <Description term='' >{'根据需要缴纳的进项税，计算应缴纳的城建和教育附加，制作一张转入营业税金及附加的凭证'}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='出纳缴纳城建税和教育附加' style={{ marginBottom: 32 }}>
          <Description term='' >{'出纳填写付款单'}</Description>
        </DescriptionList>

        <DescriptionList size="large" title='功能' style={{ marginBottom: 32 }}>
          <Description term='' >{'查询 进项税，销项税'}</Description>
          <Description term='' >{'自动生成 交税 付款单'}</Description>
          <Description term='' >{'交税 付款单确认'}</Description>
          <Description term='' >{'查询 差额'}</Description>
          <Description term='' >{'差额 自动生成免税收入/无法抵扣费用 凭证'}</Description>

          <Description term='' >{'根据实际缴纳增值税 生成 城建及教育附加 的 凭证'}</Description>
          <Description term='' >{'自动生成 交税付款单'}</Description>
          <Description term='' >{'交税 付款单确认'}</Description>

          <Description term='' >{'制作免税收入凭证'}</Description>
          <Description term='' >{'制作无法抵扣 费用凭证'}</Description>
          <Description term='' >{'制作城建及教育附加 凭证'}</Description>
          <Description term='' >{'录入 交税付款单'}</Description>
        </DescriptionList>


      </div>
    );
  }
}

export default List;





